package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.*;
import com.spider.gp.util.JsonUtils;
import com.spider.gp.util.MyQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class HignLowProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private LowHighDao lowHighDao;

    @Autowired
    private GpInfoDao gpInfoDao;

    @Override
    public void process(Page page) {
//        String reportTime = formatter.format(new Date());
//        String reportTime = "2022-08-01";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String reportTime = formatter.format(new Date());

        String url = page.getUrl().get();
        List<String> all = page.getJson().jsonPath("$.data.klines").all();
        if (all == null || all.size() == 0) {
            return;
        }
        String code = page.getJson().jsonPath("$.data.code").get();
        List<String> spjPrice = new ArrayList<>();
        List<String> spjTime = new ArrayList<>();
        LambdaQueryWrapper<GpInfo> wrapper01 = new LambdaQueryWrapper<>();
        wrapper01.eq(GpInfo::getCode,code);
        GpInfo gpInfo = gpInfoDao.selectOne(wrapper01);

        if (url.contains("secid=1.000001")){
            code = "1000001";
            gpInfo = GpInfo.builder().code("1000001").shortName("上证指数").build();
        }
        if (url.contains("secid=0.399001")){
            code = "0399001";
            gpInfo = GpInfo.builder().code("0399001").shortName("深证指数").build();
        }
        LambdaQueryWrapper<LowHigh> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LowHigh::getCode,code);
        LowHigh lowHigh = lowHighDao.selectOne(wrapper);

        //获取时间以及收盘价 数组
        for (String a: all){
            String[] split = a.split(",");
            String time  = split[0];
            spjTime.add(time);
            String spj = split[1];
            spjPrice.add(spj);
        }
        if (spjPrice.size() < 21){
            return;
        }
        if(spjPrice.size() == 21){
            System.out.println();
        }

        int a = 0 ;
        StringBuffer strLow = new StringBuffer();
        StringBuffer strHigh = new StringBuffer();
        if (lowHigh != null){
            strLow.append(lowHigh.getLowPoint());
            strHigh.append(lowHigh.getHighPoint());
        }
        for (int i = 10 ; i <= spjPrice.size() - 11 ; i ++){
            String syg10 = spjPrice.get(i-10);
            String syg9 = spjPrice.get(i-9);
            String syg8 = spjPrice.get(i-8);
            String syg7 = spjPrice.get(i-7);
            String syg6 = spjPrice.get(i-6);
            String syg5 = spjPrice.get(i-5);
            String syg4 = spjPrice.get(i-4);
            String syg3 = spjPrice.get(i-3);
            String syg2 = spjPrice.get(i-2);
            String syg1 = spjPrice.get(i-1);
            String dx = spjPrice.get(i);
            String xyg1 = spjPrice.get(i+1);
            String xyg2 = spjPrice.get(i+2);
            String xyg3 = spjPrice.get(i+3);
            String xyg4 = spjPrice.get(i+4);
            String xyg5 = spjPrice.get(i+5);
            String xyg6 = spjPrice.get(i+6);
            String xyg7 = spjPrice.get(i+7);
            String xyg8 = spjPrice.get(i+8);
            String xyg9 = spjPrice.get(i+9);
            String xyg10 = spjPrice.get(i+10);

            if (syg1.compareTo(dx) >= 0 && syg2.compareTo(dx) >= 0 && syg3.compareTo(dx) >= 0 && syg4.compareTo(dx) >= 0 &&syg5.compareTo(dx) >= 0
                    && syg6.compareTo(dx) >= 0 && syg7.compareTo(dx) >= 0 && syg8.compareTo(dx) >= 0 && syg9.compareTo(dx) >= 0 && syg10.compareTo(dx) >= 0 &&
                    xyg1.compareTo(dx) > 0 && xyg2.compareTo(dx) > 0 && xyg3.compareTo(dx) > 0 && xyg4.compareTo(dx) > 0 && xyg5.compareTo(dx) > 0
                    && xyg6.compareTo(dx) > 0 && xyg7.compareTo(dx) > 0 && xyg8.compareTo(dx) > 0 && xyg9.compareTo(dx) > 0 && xyg10.compareTo(dx) > 0   ){
                a = i; //找到最低点坐标
                strLow.append(spjTime.get(i).replace("-","")+","+spjPrice.get(i)+","+i+";");
            }else {
                //还没有最低点
            }

            if (syg1.compareTo(dx) <= 0 && syg2.compareTo(dx) <= 0 && syg3.compareTo(dx) <= 0 && syg4.compareTo(dx) <= 0 &&syg5.compareTo(dx) <= 0
                    && syg6.compareTo(dx) <= 0 && syg7.compareTo(dx) <= 0 && syg8.compareTo(dx) <= 0 && syg9.compareTo(dx) <= 0 && syg10.compareTo(dx) <= 0 &&
                    xyg1.compareTo(dx) < 0 && xyg2.compareTo(dx) < 0 && xyg3.compareTo(dx) < 0 && xyg4.compareTo(dx) < 0 && xyg5.compareTo(dx) < 0
                    && xyg6.compareTo(dx) < 0 && xyg7.compareTo(dx) < 0 && xyg8.compareTo(dx) < 0 && xyg9.compareTo(dx) < 0 && xyg10.compareTo(dx) < 0   ){
                a = i; //找到最高点坐标
                strHigh.append(spjTime.get(i).replace("-","")+","+spjPrice.get(i)+","+i+";");
            }else {
                //还没有最高点
            }

        }

        if (lowHigh == null){
            //第一次新增
            lowHighDao.insert(LowHigh.builder().reportTime(reportTime).code(code).name(gpInfo.getShortName()).lowPoint(strLow.toString()).highPoint(strHigh.toString()).build());
        }else {
            lowHighDao.updateById(LowHigh.builder().reportTime(reportTime).code(code).name(gpInfo.getShortName()).lowPoint(strLow.toString()).highPoint(strHigh.toString()).build());
        }


    }

    @Override
    public Site getSite() {
        return site;
    }
}








